/**
 * Created by caosh on 2018/5/13 0013.
 */
var RentalIndex = new Vue({
    el: '#rental-index',
    computed: {
        allTotalHouse: function () {
            var totalHouse = 0;

            for (var i=0; i<this.houseSrcList.length; i++) {
                totalHouse += this.houseSrcList[i].totalHouse;
            }

            return totalHouse;
        },
        rooms: function () {
            var roomList = [];
            if (this.filterHouse == 'all') {
                roomList = this.roomLists;
            } else {
                for (var i=0, len=this.roomLists.length; i<len; i++) {
                    var room = this.roomLists[i];
                    if (room.houseSrcId == this.filterHouse ) {
                        roomList.push(room);
                    }
                }
            }

            return roomList;
        }
    },
    data: {
        houseSrcList: [],
        roomLists: [],
        show: '',
        houseSrc: {settings:[]},
        tempList: [],
        filterHouse: 'all',
        room: {},
        chooseHouse: {roomList:[]},
        renterId: '',
        signProtocol: {
            'rentWay': {}
        },
        chargeStandard:[{'k':0, 'v':'抄表收费'},{'k':1, 'v':'固定每月收费'}],
        detail: {
            house: {},
            room: {}
        },
        currentProtocol:{
            'idPic': [],
            'rentWay': {},
            'incidental': []
        },
        historyProtocol: [],
        bills: {
            'pending':{},
            'future': [],
            'history': []
        },
        refund: {
            date: '',
            detail: '',
            money: 0
        },
        billDetail: {
            info: []
        },
        showBillDetail: false,
        showDeleteBill: false
    },
    filters: {
        overview: function(list, v) {
            var total = 0;
            if (list.length == 0) {
                total = 0;
            } else {
                for(var i=0, len=list.length; i<len; i++) {
                    var house = list[i],
                        roomList = house.roomList;
                    for (var j=0, l=roomList.length; j<l; j++) {
                        var room = roomList[j];
                        if (room.status == v) {
                            total++;
                        }
                    }
                }
            }

            return total;
        },
        rent: function (list, v) {
            var total = 0;
            if (list.length == 0) {
                total = 0;
            } else {
                for(var i=0, len=list.length; i<len; i++) {
                    var house = list[i],
                        roomList = house.roomList;
                    for (var j=0, l=roomList.length; j<l; j++) {
                        var room = roomList[j];
                        if (room.status != v) {
                            total++;
                        }
                    }
                }
            }

            return total;
        }
    },
    methods: {
        getHouseSrcList: function () {
            var _this = this;
            _this.setRequestDatas(null);
            _this.ajaxOptions.url = '/getHouseSrcList';
            _this.ajaxOptions.done = function (result, status, jqXHR) {
                if (result.retCode == 0) {
                    _this.houseSrcList = result.data;
                } else {
                    console.log(result.errMsg);
                }
            };

            _this.doAjax();
        },
        getRoomLists: function () {
            for (var i=0; i<this.houseSrcList.length; i++) {
                this.getRoomList(this.houseSrcList[i].houseSrcId);
            }
        },
        getRoomList: function (houseSrcId) {
            var _this = this;
            _this.setRequestDatas({'houseSrcId': houseSrcId});
            _this.ajaxOptions.data = this.requestDatas;
            _this.ajaxOptions.url = '/getRoomList';
            _this.ajaxOptions.done = function (result, status, jqXHR) {
                if (result.retCode == 0) {
                    _this.roomLists.push(result.data);
                } else {
                    console.log(result.errMsg);
                }
            };

            _this.doAjax();
        },
        getTempList: function () {
            var _this = this;
            _this.setAjaxOptions('/getTempList', null, function (result, status, jqXHR) {
                if (result.retCode == 0) {
                    _this.tempList = result.data;
                } else {
                    console.log(result.errMsg);
                }
            });
            _this.doAjax();
        },
        addHouseSrc: function () {
            var _this = this;
            _this.setAjaxOptions('/addHouseSrc', _this.houseSrc, function (result, status, jqXHR) {
                if (result.retCode == 0) {
                    console.log('success');
                    _this.show = '';
                } else {
                    console.log(result.errMsg);
                }
            });
            _this.doAjax();
        },
        openEdit: function (houseSrc) {
            var _this = this;
            if (_this.show == '') {
                _this.setAjaxOptions('/getHouseSrcDetail', {'houseSrcId': houseSrc.houseSrcId}, function (result, status, jqXHR) {
                    if (result.retCode == 0) {
                        _this.houseSrc.houseSrcName = result.data.houseName;
                        _this.houseSrc.houseSrcId = result.data.houseSrcId;
                        _this.houseSrc.settings = result.data.temp;
                        _this.show = 'edit';
                    } else {
                        console.log(result.errMsg);
                    }
                });
                _this.doAjax();
            }
        },
        eidtHouseSrc: function () {
            var _this = this;
            _this.setAjaxOptions('/eidtHouseSrc', _this.houseSrc, function (result, status, jqXHR) {
                if (result.retCode == 0) {
                    console.log('success');
                    _this.show = '';
                } else {
                    console.log(result.errMsg);
                }
            });
            _this.doAjax();
        },
        openAddRoom: function (h) {
            if (this.show == '') {
                this.show = 'addRoom';
                this.chooseHouse = h;
                this.room.houseSrcId = h.houseSrcId;
            }
        },
        addRoom: function () {
            var _this = this;
            _this.setAjaxOptions('/addRoom', _this.room, function (result, status, jqXHR) {
                if (result.retCode == 0) {
                    console.log('success');
                    _this.show = '';
                } else {
                    console.log(result.errMsg);
                }
            });
            _this.doAjax();
        },
        resetHouseSrc: function () {
            this.houseSrc = {
                'houseSrcName': '',
                    'floor': '',
                    'roomNum': '',
                    'settings': [
                    {
                        'code': 'fee_sf',
                        'name': '水费',
                        'modelId': ''
                    },
                    {
                        'code': 'fee_df',
                        'name': '电费',
                        'modelId': ''
                    },
                    {
                        'code': 'fee_rqf',
                        'name': '燃气费',
                        'modelId': ''
                    },
                    {
                        'code': 'fee_wyf',
                        'name': '物业费',
                        'modelId': ''
                    },
                    {
                        'code': 'fee_fwf',
                        'name': '服务费',
                        'modelId': ''
                    },
                    {
                        'code': 'fee_bjf',
                        'name': '保洁费',
                        'modelId': ''
                    },
                    {
                        'code': 'fee_wxf',
                        'name': '维修费',
                        'modelId': ''
                    },
                    {
                        'code': 'fee_clf',
                        'name': '材料费',
                        'modelId': ''
                    },
                    {
                        'code': 'fee_dsf',
                        'name': '电视费',
                        'modelId': ''
                    },
                    {
                        'code': 'fee_kdf',
                        'name': '宽带费',
                        'modelId': ''
                    },
                    {
                        'code': 'fee_wsf',
                        'name': '卫生费',
                        'modelId': ''
                    },
                    {
                        'code': 'fee_qtf',
                        'name': '其他费用',
                        'modelId': ''
                    }
                ]
            }
        },
        resetRoom: function () {
            this.room = {
                'houseSrcId': '',
                'roomName': ''
            }
        },
        resetSignProtocol: function () {
            this.signProtocol = {
                'roomId': '',
                'renterName': '',
                'phone': '',
                'idNumber': '',
                'idPic': [
                ],
                'startDate': '',
                'endDate': '',
                'rental': 0,
                'deposit': 0,
                'payWay': 0,
                'rentWay': {
                    'way': 0,
                    'day': 0,
                    'billPeriod': 0
                },
                'billPeriod': 0,
                'incidental ': [{
                    'code':'fee_fz',
                    'chargeStandard':0,
                    'meterNumber':0,
                    'price':0,
                    'meterDate':''
                }, {
                    'code':'fee_sf',
                    'chargeStandard':0,
                    'meterNumber':0,
                    'price':0,
                    'meterDate':''
                }, {
                    'code':'fee_df',
                    'chargeStandard':0,
                    'meterNumber':0,
                    'price':0,
                    'meterDate':''
                }, {
                    'code':'fee_rqf',
                    'chargeStandard':0,
                    'meterNumber':0,
                    'price':0,
                    'meterDate':''
                }, {
                    'code':'fee_wyf',
                    'chargeStandard':0,
                    'meterNumber':0,
                    'price':0,
                    'meterDate':''
                }, {
                    'code':'fee_fwf',
                    'chargeStandard':0,
                    'meterNumber':0,
                    'price':0,
                    'meterDate':''
                }, {
                    'code':'fee_bjf',
                    'chargeStandard':0,
                    'meterNumber':0,
                    'price':0,
                    'meterDate':''
                }, {
                    'code':'fee_wxf',
                    'chargeStandard':0,
                    'meterNumber':0,
                    'price':0,
                    'meterDate':''
                }, {
                    'code':'fee_clf',
                    'chargeStandard':0,
                    'meterNumber':0,
                    'price':0,
                    'meterDate':''
                }, {
                    'code':'fee_dsf',
                    'chargeStandard':0,
                    'meterNumber':0,
                    'price':0,
                    'meterDate':''
                }, {
                    'code':'fee_kdf',
                    'chargeStandard':0,
                    'meterNumber':0,
                    'price':0,
                    'meterDate':''
                }, {
                    'code':'fee_wsf',
                    'chargeStandard':0,
                    'meterNumber':0,
                    'price':0,
                    'meterDate':''
                }, {
                    'code':'fee_qtf',
                    'chargeStandard':0,
                    'meterNumber':0,
                    'price':0,
                    'meterDate':''
                }]
            }
        },
        resetDetail: function () {
            this.detail = {
                house: {},
                room: {}
            }
        },
        resetCurrentProtocol: function () {
            this.currentProtocol = {
                'idPic': [],
                'rentWay': {},
                'incidental': []
            }
        },
        resetHistoryProtocol: function () {
            this.historyProtocol = [];
        },
        resrtBills: function () {
            this.bills = {
                'pending':{},
                'future': [],
                'history': []
            }
        },
        openUrgePayRent: function (renterId) {
            if (this.show == '') {
                this.show = 'remind';
            }
        },
        openSignProtocol: function (h, r) {
            if (this.show == '') {
                this.show = 'contract';
                this.signProtocol.houseName = h.houseSrcName;
                this.signProtocol.roomName = r.roomName;
                this.signProtocol.roomId = r.roomId;
            }
        },
        openDetial: function (h, r) {
            if (this.show == '') {
                this.show = 'detail';
                this.detail.house = h;
                this.detail.room = r;
                this.getCurrentProtocol(r.roomId);
                this.getHistoryProtocol(r.roomId);
                this.getBills(r.roomId);
            }
        },
        getCurrentProtocol: function (roomId) {
            var _this = this;
            _this.setAjaxOptions('/getCurrentProtocol', {'roomId': roomId}, function (result, status, jqXHR) {
                if (result.retCode == 0) {
                    _this.currentProtocol = result.data;
                } else {
                    console.log(result.errMsg);
                }
            });

            _this.doAjax();
        },
        getHistoryProtocol: function (roomId) {
            var _this = this;
            _this.setAjaxOptions('/getHistoryProtocol', {'roomId': roomId}, function (result, status, jqXHR) {
                if (result.retCode == 0) {
                    _this.historyProtocol = result.data;
                } else {
                    console.log(result.errMsg);
                }
            });

            _this.doAjax();
        },
        getBills: function (roomId) {
            var _this = this;
            _this.setAjaxOptions('/getBills', {'roomId': roomId}, function (result, status, jqXHR) {
                if (result.retCode == 0) {
                    _this.bills = result.data;
                } else {
                    console.log(result.errMsg);
                }
            });

            _this.doAjax();
        },
        refundRent: function () {
            var _this = this;
            _this.refund.protocolId = _this.currentProtocol.protocolId;
            _this.refund.roomId = _this.currentProtocol.roomId;
            _this.setAjaxOptions('/refundRent', _this.refund, function (result, status, jqXHR) {
                if (result.retCode == 0) {
                    _this.show = 'refundSuccess';
                } else {
                    _this.show = 'refundFailed';
                }
            });

            _this.doAjax();
        },
        getBillDetail: function (billId) {
            var _this = this;

            if (_this.showBillDetail) {
                return;
            }

            _this.showBillDetail = !_this.showBillDetail;

            _this.refund.protocolId = _this.currentProtocol.protocolId;
            _this.refund.roomId = _this.currentProtocol.roomId;
            _this.setAjaxOptions('/getBillDetail', _this.refund, function (result, status, jqXHR) {
                if (result.retCode == 0) {
                    _this.billDetail = result.data;
                } else {
                    console.log(result.errMsg);
                }
            });

            _this.doAjax();
        },
        urgePayRent: function (renterId) {
            var _this = this;
            _this.setAjaxOptions('/urgePayRent', {'renterId': renterId}, function (result, status, jqXHR) {
                if (result.retCode == 0) {
                    _this.show = 'remind';
                }
            });
            _this.doAjax();
        },
        openDelBill: function (bill) {
            if (!this.showDeleteBill) {
                this.showDeleteBill = !this.showDeleteBill;
            }
        }
    },
    mixins: [AjaxMixin, UserMixin],
    mounted: function () {
        Vue.useLayerElement();
        this.resetSignProtocol();
        this.resetHouseSrc();
        this.resetRoom();
        this.getHouseSrcList();
        this.getTempList();
        Vue.useLayerForm();
    },
    watch: {
        houseSrcList: {
            handler: function (val, oldVal) {
                this.getRoomLists();
            },
            deep: true
        },
        show: function (val, oldVal) {
            if (val == '') {
                this.resetHouseSrc();
                this.resetRoom();
                this.resetSignProtocol();
                this.resetDetail();
                this.resetCurrentProtocol();
                this.resetHistoryProtocol();
            } else if (val == 'add') {
                Vue.useLayerForm();
            } else if (val == 'contract') {
                Vue.useLayerForm();
                Vue.useLayerUpload(['upload1', 'upload2', 'upload3'], '');
                Vue.useLayerDate(['startDate', 'endDate'], this.signProtocol);
            } else if (val == 'detail') {
                Vue.useLayerForm();
                Vue.useLayerDate(['date'], this.signProtocol);
            }
        }
    }
});